//验证token
function fun() {
    var token = sessionStorage.getItem("token");
    if (token == '' || token == 'undefined' || token == undefined || token == NaN || token == 'NaN') {
        parent.location.href = "login.html";
        return false;
    }
    return true;
}
var params = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "首页"
};
var params1 = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "快速入门"
};
var params2 = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "产品服务"
};
var params3 = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "关于我们"
};
var params4 = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "查询服务"
};
var params5 = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "新闻动态"
};
var params6 = {
    'apiKey': 'api_post_getSwiperBanners',
    'deviceType': 'DIC_DEVICE_TYPE_1',
    contentType: "application/json;charset=utf-8",
    datatype: 'json',
    banner_key: "个人中心"
};

var token = sessionStorage.getItem('token');
$(function () {
    //添加个人中心图标
    if ((token == '' || token == 'undefined' || token == undefined || token == NaN || token == 'NaN')) {
        $('.Landing').text("登录");
        $('.login').text("注册");
        $('.Landing + span').text("|");
    } else {
        $('.Landing,.login,.Landing + span').text("");
        $('.user').show();
        $('.exit').text("退出");
        $('.line').text("|");
    }
    //退出登录
    $(".exit").on("click", function () {
        sessionStorage.clear();   //清除所有session值
        window.location.reload();
        parent.location.href = "Home.html";
    })

    //下拉导航
    $('.nav-bar > li').on('mouseover', function () {
        $(this).children('.nav_left').slideDown(300);
    })
    $('.nav-bar > li').on('mouseleave', function () {
        $(this).children('.nav_left').stop(true, false).slideUp();
    })

    if (/Home.html|^\/$/.test(parent.window.location.pathname)) {
        var p1 = new Promise(function (resolve, reject) {
            ajax('post', '/d_easy/deasy/api', params, 'json', function arr(data) {
                if (data.returnCode == 1) {
                    resolve(data.bannerlist.length);
                    $(".swiper-wrapper").html("");
                    for (var i = 0; i < data.bannerlist.length; i++) {
                        $(".swiper-wrapper").append('<div class="swiper-slide swiper-slide1"><a href="javascript:void(0)" class="imgBox" style="background-image: url(http://deasy.dsy56.cn/d_easy' + data.bannerlist[i].img_url + '"></a></div>');
                    }
                }
            })
        })
        p1.then(function (value) {
            var swiper1 = new Swiper('.swiper-container1', {
                pagination: '.swiper-pagination1',
                preventClicks: true,
                paginationHide: true,
                clickable: true,
                slideToClickedSlide: true,
                autoplay: 4000,
                paginationClickable: true,
                effect: 'fade',
                mousewheelControl: false,
                autoplayDisableOnInteraction: false,
                observer: true,//修改swiper自己或子元素时，自动初始化swiper
                observeParents: true//修改swiper的父元素时，自动初始化swiper
            });
        }, function (reason) {
        });
    }
    function banners(path, params) {
        if (path.test(parent.window.location.pathname)) {
            ajax('post', '/d_easy/deasy/api', params, 'json', function (data) {
                if (data.returnCode == 1) {
                    $(".swiper-wrapper").html("");
                    for (var i = 0; i < data.bannerlist.length; i++) {
                        $(".swiper-wrapper").append('<div class="swiper-slide swiper-slide1"><a href="#" class="imgBox" style="background-image: url(http://deasy.dsy56.cn/d_easy' + data.bannerlist[i].img_url + '"></a></div>');
                    }
                }
            })
        }
    }
    //快速入门
    banners(/QuickStart.html/, params1);
    //产品服务
    banners(/ProductService.html/, params2);
    //关于我们
    banners(/AboutUs.html/, params3);
    //查询服务
    banners(/QuerServices.html/, params4);
    //新闻动态
    banners(/NewsDetails.html/, params5);
    //新闻动态
    banners(/NewsInformation.html/, params5);
    //个人中心
    banners(/Basic_Info.html/, params6);
    banners(/New_order.html/, params6);
    //我的订单
    banners(/MyOrder.html/, params6);
});

//登录超时
if (!(token == '' || token == 'undefined' || token == undefined || token == NaN || token == 'NaN')) {
    var EXPIRE_IN_15MIN = 1000 * 60 * 120; // 120 min
    var logoutTimer = setTimeout(logout, EXPIRE_IN_15MIN);
    function userActionHandler() {
        clearTimeout(logoutTimer);
        logoutTimer = setTimeout(logout, EXPIRE_IN_15MIN);
    }
    function logout() {
        alert("您的登录已超时, 请点确定后重新登录!");
        sessionStorage.clear()   //清除所有session值
        parent.location.href = 'login.html';
        //window.location.reload()
        // do your logout logic here
    }
    // TODO: bind userActionHandler to any 'user action' you need
};